package org.orbis.flag;

import org.msgpack.MessagePackObject;
import org.msgpack.object.ArrayType;
import org.msgpack.object.BooleanType;
import org.msgpack.object.FloatType;
import org.msgpack.object.IntegerType;
import org.orbis.structure.ContentInfo;

public class FlagSet{
	private boolean[] boolFlag;
	private int[] integerFlag;
	private long[] longFlag;
	private float[] floatFlag;

	public FlagSet(ContentInfo contentInfo){
		boolFlag = new boolean[contentInfo.binaryFlagNum()];
		integerFlag = new int[contentInfo.integerFlagNum()];
		longFlag = new long[contentInfo.longFlagNum()];
		floatFlag = new float[contentInfo.floatFlagNum()];
	}

	public FlagSet(ArrayType packed){
		MessagePackObject[] objs = packed.asArray();
		boolFlag = new boolean[objs[0].asInt()];
		integerFlag = new int[objs[1].asInt()];
		longFlag = new long[objs[2].asInt()];
		floatFlag = new float[objs[3].asInt()];
		
		int subtotal = 4;
		for(int i = 0; i < boolFlag.length; i++){
			boolFlag[i] = objs[subtotal + i].asBoolean();
		}
		subtotal += boolFlag.length;
		for(int i = 0; i < integerFlag.length; i++){
			integerFlag[i] = objs[subtotal + i].asInt();
		}
		subtotal += integerFlag.length;
		for(int i = 0; i < longFlag.length; i++){
			longFlag[i] = objs[subtotal + i].asLong();
		}
		subtotal += longFlag.length;
		for(int i = 0; i < floatFlag.length; i++){
			floatFlag[i] = objs[subtotal + i].asFloat();
		}
	}

	public ArrayType pack(){
		MessagePackObject[] objs =
			new MessagePackObject[boolFlag.length + integerFlag.length + longFlag.length + floatFlag.length
				+ 4];
		objs[0] = IntegerType.create(boolFlag.length);
		objs[1] = IntegerType.create(integerFlag.length);
		objs[2] = IntegerType.create(longFlag.length);
		objs[3] = IntegerType.create(floatFlag.length);

		int subtotal = 4;
		for(int i = 0; i < boolFlag.length; i++)
			objs[subtotal + i] = BooleanType.create(boolFlag[i]);
		subtotal += boolFlag.length;
		for(int i = 0; i < integerFlag.length; i++)
			objs[subtotal + i] = IntegerType.create(integerFlag[i]);
		subtotal += integerFlag.length;
		for(int i = 0; i < longFlag.length; i++)
			objs[subtotal + i] = IntegerType.create(longFlag[i]);
		subtotal += longFlag.length;
		for(int i = 0; i < floatFlag.length; i++)
			objs[subtotal + i] = FloatType.create(floatFlag[i]);

		return ArrayType.create(objs);
	}
}
